import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { isEmpty } from 'lodash';
import { ApiException } from 'src/core/exceptions/api.exception';
import Resume from 'src/entities/resume.entity';
import Student from 'src/entities/student.entity';
import { Repository } from 'typeorm';
import { CreateResumeDto } from './resume.dto';

@Injectable()
export class ResumeService {
  constructor(
    @InjectRepository(Resume) private resumeRepository: Repository<Resume>,
    @InjectRepository(Student) private studentRepository: Repository<Student>,
  ) {}

  async list(uid: string) {
    const student = await this.studentRepository.findOne({ where: { userId: uid } });
    if (isEmpty(student)) {
      throw new ApiException(12001);
    }
    return await this.resumeRepository.find({ where: { student_id: student.id } });
  }

  async create(uid: string, createResumeDto: CreateResumeDto) {
    const student = await this.studentRepository.findOne({ where: { userId: uid } });
    if (isEmpty(student)) {
      throw new ApiException(12001);
    }

    // 简历只能上传三份
    // const resumes = await this.resumeRepository.find({ where: { student_id: student.id } });
    // if (resumes.length >= 3) {
    //   throw new ApiException(12002);
    // }

    return await this.resumeRepository.save({ ...createResumeDto, student_id: student.id });
  }

  async remove(id: number) {
    const resume = await this.resumeRepository.findOne({ where: { id } });
    if (isEmpty(resume)) {
      throw new ApiException(12003);
    }
    await this.resumeRepository.delete(id);
    return 'ok';
  }
}
